import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel2 extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");

    private JTextField transname = new JTextField();
    private JTextField transmoney = new JTextField();

    public TransPanel2(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel namelable = new JLabel("请输入转账人姓名：");
        namelable.setBounds(200, 200, 150, 30);
        namelable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(namelable);

        transname.setBounds(370, 200, 100, 30);
        transname.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transname);

        JLabel moneylable = new JLabel("请输入转账金额：");
        moneylable.setBounds(200, 250, 150, 30);
        moneylable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(moneylable);

        transmoney.setBounds(370, 250, 100, 30);
        transmoney.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transmoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 300, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 300, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            // 获取输入的对方账号
            String otherAccount = transname.getText();
            double balance = 0;
            try {
                // 获取输入的转账金额
                balance = Double.parseDouble(transmoney.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }
            // 当前登录的账户
            Util db = new Util();
            User loginUser = db.getById(atm.getLoginUser().getId());
            User otherUser = db.getByUsername(otherAccount);
            //判断是否为自己
            if (transname.getText().equals(atm.getUsers().get(atm.getNum()).getUsername())) {
                JOptionPane.showMessageDialog(this, "不能转账给自己");
                return;
            }
            // 判断账号是否存在
            if (otherUser == null) {
                JOptionPane.showMessageDialog(this, "查无此人");
                return;
            }
            // 判断余额是否足够
            if (balance > loginUser.getMoney()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
//            // 当前账户扣钱
            loginUser.setMoney(loginUser.getMoney() - balance);
//            // 对方账户加钱
            otherUser.setMoney(otherUser.getMoney() + balance);

            db.testTransition(loginUser, otherUser);
            JOptionPane.showMessageDialog(this, "转账成功");
            atm.switchPanel("menu");

        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("login");
        }


    }
}


